<div class="layui-fluid" lay-title="文章分类">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form-group">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级分类</label>
                    <div class="layui-input-block">
                        <input type="text" id="tree" name="tree" lay-filter="tree" autocomplete="off" placeholder="主目录" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">显示顺序</label>
                    <div class="layui-input-block">
                        <input type="text" name="orderNum" lay-verify="required|number" autocomplete="off" placeholder="请输入显示顺序" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">有效</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" value="1" title="普通" checked="">
                        <input type="radio" name="type" value="2" title="热门">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="form-group-submit">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    layui.use(['treeSelect', 'admin', 'form'], function () {
        var treeSelect = layui.treeSelect
            , $ = layui.$
            , admin = layui.admin
            , layer = layui.layer
            , form = layui.form;
        var cmsCategory = {};
        var id = POPUP_DATA.id;
        var parentId = POPUP_DATA.parentId;
        var treeGrid = POPUP_DATA.treeGrid;
        if (id) {
            admin.get({
                url: '/cmsCategory/info/' + id,
                async: false,
                success: function (res) {
                    if (res.code == 0) {
                        cmsCategory = res.category;
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
        }
        treeSelect.render({
            // 选择器
            elem: '#tree',
            // 数据
            data: '/cmsCategory/queryCategoryList?token=' + admin.loginToken,
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '主目录',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 点击回调
            click: function (d) {
                console.log(d);
            },
            // 加载完成后的回调函数
            success: function (d) {
                if (id) {
                    if (cmsCategory.parentId != 0) {
                        treeSelect.checkNode('tree', cmsCategory.parentId);
                    }
                    $("#tree").val(cmsCategory.parentId);
                }
                if (parentId) {
                    $("#tree").val(parentId);
                    treeSelect.checkNode('tree', parentId);
                }
            }
        });

        form.render(null, 'form-group');

        form.val("form-group", {
            "name": cmsCategory.name // "name": "value"
            , "orderNum": cmsCategory.orderNum
            , "type": cmsCategory.type
        });

        /* 监听提交 */
        form.on('submit(form-group-submit)', function (data) {
            var field = data.field;
            var categoryData = {};
            var url = "/cmsCategory/save";
            if (id) {
                url = "/cmsCategory/update?_method=PUT";
                categoryData.id = id;
            }
            var cParentId = field.tree;
            if (cParentId != null && cParentId != "") {
                categoryData.parentId = cParentId;
            }
            categoryData.type = field.type;
            categoryData.name = field.name;
            categoryData.orderNum = field.orderNum;

            admin.post({
                url: url,
                contentType: "application/json",
                data: JSON.stringify(categoryData),
                success: function (res) {
                    if (res.code == 0) {
                        // 提醒保存成功，关闭当前弹窗
                        layer.msg(res.msg);
                        layer.closeAll();
                        //刷新
                        treeGrid.reload("list-table-cmsCategory");
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });
</script>